//
//  UIViewController+TopMostViewController.m
//  SECRET
//
//  Created by 徐汉卿 on 15/11/10.
//  Copyright © 2015年 Jessica. All rights reserved.
//

#import "UIViewController+TopMostViewController.h"

@implementation UIViewController (TopMostViewController)



//19_5_15这个方法拿不到当前控制器 ,因为rootvc 一般是tabbarVC tabbarVC.presentedViewController 都是 空,但是拿到tabbarVC 跳出控制器还是没问题的
+(UIViewController *)currentViewController
{
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [rootVC topMostViewController];
}

- (UIViewController *)topMostViewController
{
    if (self.presentedViewController == nil)
    {
        return self;
    }
    else if ([self.presentedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navigationController = (UINavigationController *)self.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [lastViewController topMostViewController];
    }
    
    UIViewController *presentedViewController = (UIViewController *)self.presentedViewController;
    return [presentedViewController topMostViewController];
}

@end

#pragma mark -

@implementation UIApplication (TopMostViewController)

- (UIViewController *)topMostViewController
{
    return [self.keyWindow.rootViewController topMostViewController];
}

@end
